package com.cimr.comm.controller.manager;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.cimr.comm.bo.OperationBo;
import com.cimr.comm.dto.HttpResult;
import com.cimr.comm.dto.LayuiTableData;
import com.cimr.comm.service.GroupService;
import com.cimr.comm.service.OperationService;
import com.cimr.comm.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liqi on 2018/2/1.
 * liqiwork@qq.com
 */
@Controller
@RequestMapping("/operationManager/ajax")
public class OperationManagerAjaxController {

    @RequiresPermissions(value = {"manager_operationManager", "operationManager:getOperationList", "cimrFrame:operation:passkey"}, logical = Logical.OR)
    @RequestMapping({"getOperationList"})
    @ResponseBody
    public LayuiTableData getOperationList(@RequestParam(required=true,defaultValue="1") Integer page,
                                                     @RequestParam(required=false,defaultValue="10") Integer limit,
                                                     @RequestParam(required = false) String operationName) {

        LayuiTableData result = new LayuiTableData();
        PageHelper.startPage(page, limit);
        List list = operationService.getOperationListWithName(operationName);
        if (list!=null && list.size()>0) {
            PageInfo pageInfo = new PageInfo(list);
            List list1 = operationService.getOperationListWithName(operationName);
            result.setCount(list1.size());
            result.setData(pageInfo.getList());
        }
        result.setCode(0);
        result.setMsg("");
        return result;
    }

    @RequiresPermissions(value = {"operationManager:give", "operationManager:getOperationListByCurrUser", "cimrFrame:operation:passkey"}, logical = Logical.OR)
    @RequestMapping({"getOperationListByCurrUser"})
    @ResponseBody
    public LayuiTableData getOperationListByCurrUser(@RequestParam(required=true,defaultValue="1") Integer page,
                                           @RequestParam(required=false,defaultValue="10") Integer limit,
                                           @RequestParam(required = false) String operationName) {

        LayuiTableData result = new LayuiTableData();
//        User currentUser = TokenUtil.getToken();
//        List<Group> groupList = groupService.getGroupAndSonsByGroupId(currentUser.getGroupId());
//        if (groupList!=null && groupList.size()>0) {
//            String[] groupIds = new String[groupList.size()];
//            for (int i = 0; i < groupList.size(); i++) {
//                groupIds[i] = groupList.get(i).getId();
//            }
            PageHelper.startPage(page, limit);
            List list = operationService.getOperationListWithNameByCurrUser(operationName);
            if (list!=null && list.size()>0) {
                PageInfo pageInfo = new PageInfo(list);
                List list1 = operationService.getOperationListWithNameByCurrUser(operationName);
                result.setCount(list1.size());
                result.setData(pageInfo.getList());
            }
//        }

        result.setCode(0);
        result.setMsg("");
        return result;
    }

    /**
     *@auther zengfan
     *@date 2018/1/10 16:07
     *@params  * @param null
     *根据id查询已经分配的操作
     **/
    @RequiresPermissions(value = {"operationManager:give", "operationManager:getOperationByRoleId", "cimrFrame:operation:passkey"}, logical = Logical.OR)
    @RequestMapping({"getOperationByRoleId"})
    @ResponseBody
    public HttpResult getOperationByRoleId(@RequestParam(required = true) String id) {
        List<OperationBo> currentList = operationService.getOperationListByRoleId(id);
        HttpResult result = new HttpResult(true, "");
        result.setData(currentList);
        return result;
    }

    /**
     * 取操作授权穿梭框的数据(用于角色配置时的操作授权)
     * @param id
     * @return
     */
    @RequestMapping({"getOperationByRoleIdTransfer4Role"})
    @ResponseBody
    public HttpResult getOperationByRoleIdTransfer4Role(@RequestParam(required = true) String id) {
        HttpResult result = new HttpResult(true, "");
        List<OperationBo> haveOperations = operationService.getOperationListByRoleId(id);
        JSONArray haveOperationsJSONArray = new JSONArray();
        for (int i = 0; i < haveOperations.size(); i++) {
            haveOperationsJSONArray.add(haveOperations.get(i).getId());
        }

        List allOperations = operationService.getOperationListWithNameByCurrUser("");
        JSONArray allOperationsJSONArray = new JSONArray();
        for (int i = 0; i < allOperations.size(); i++) {
            JSONObject json = new JSONObject();
            json.put("value", ((OperationBo)allOperations.get(i)).getId());
            json.put("title", ((OperationBo)allOperations.get(i)).getOperationName());
            allOperationsJSONArray.add(json);
        }

        Map resultMap = new HashMap();
        resultMap.put("haveOperations", haveOperationsJSONArray);
        resultMap.put("allOperations", allOperationsJSONArray);
        result.setData(resultMap);
        return result;
    }

    @RequiresPermissions(value = {"operationManager:edit", "operationManager:getOperationById", "cimrFrame:operation:passkey"}, logical = Logical.OR)
    @RequestMapping({"getOperationById"})
    @ResponseBody
    public HttpResult getOperationById(@RequestParam(required = true) String id) {
        OperationBo operationBo = operationService.getOperationById(id);
        HttpResult result = new HttpResult(true, "");
        result.setData(operationBo);
        return result;
    }

    @RequiresPermissions(value = {"operationManager:save",
                                  "operationManager:add",
                                  "operationManager:saveOperation",
                                  "cimrFrame:operation:passkey"}, logical = Logical.OR)
    @RequestMapping({"saveOperation"})
    @ResponseBody
    public HttpResult saveOperation(String id,String operationKey,String operationName,String comment,Float orderId){
        int resultStatus1;
        if(id != null && !"".equals(id)) {
            resultStatus1 = operationService.updateOperation(id,operationKey,operationName,comment,orderId);
        } else {
            resultStatus1 =  operationService.insertOperation(IdUtil.getId().toString(),operationKey,operationName,comment,orderId);
        }

        HttpResult result = new HttpResult(true, "");
        if(resultStatus1 == 0) {
            result.setSuccess(false);
        }
        return result;
    }

    @RequiresPermissions(value = {"operationManager:delete", "operationManager:deleteOperation", "cimrFrame:operation:passkey"}, logical = Logical.OR)
    @RequestMapping({"deleteOperation"})
    @ResponseBody
    public HttpResult deleteOperation(String id){
        int resultStatus1;
        resultStatus1 =  operationService.deleteOperationById(id);
        HttpResult result = new HttpResult(true, "");
        if(resultStatus1 == 0) {
            result.setSuccess(false);
        }
        return result;
    }

    @RequiresPermissions(value = {"operationManager:save",
                                  "operationManager:add",
                                  "operationManager:getOperationListByKey",
                                  "cimrFrame:operation:passkey"}, logical = Logical.OR)
    @RequestMapping({"getOperationListByKey"})
    @ResponseBody
    public HttpResult getOperationListByKey(String operationKey) {
        HttpResult result = new HttpResult(true, "");
        List<OperationBo> list =  operationService.getOperationListByKey(operationKey);

        if(list!=null&&list.size()==0){
            result.setData(null);
        }else{
            result.setData(list);
        }
        return result;
    }

    @RequiresPermissions(value = {"operationManager:save",
                                  "operationManager:add",
                                  "operationManager:getOperationListByKeyOutCurrent",
                                  "cimrFrame:operation:passkey"}, logical = Logical.OR)
    @RequestMapping({"getOperationListByKeyOutCurrent"})
    @ResponseBody
    public HttpResult getOperationListByKeyOutCurrent(String id,String operationKey) {
        HttpResult result = new HttpResult(true, "");
        List<OperationBo> list =  operationService.getOperationListByKey(operationKey);
        if(list!=null&&list.size()!=0){
            if (list.get(0).getId().equals(id)){
                result.setData(null);
            }else {
                result.setData(list);
            }
        }else{
            result.setData(null);
        }
        return result;
    }

    @Autowired
    private OperationService operationService;
    @Autowired
    private GroupService groupService;
}
